const BaseRest = require('../rest.js');
const jwt = require('jsonwebtoken')
const svgCaptcha = require('svg-captcha');
const axioss = require('../../utils/axios-stock')


module.exports = class extends BaseRest {
  async getAction() {
      if(this.id === 'env'){
        // console.log('ENV', this.ctx.config('stockURL'))
        let res = await axioss.get('/stock_index')
        console.log('data', res.data)
        this.ctx.body = {
          code: 0,
          data: res.data
        }
        return
      }
      if(this.id === 'add') {
        console.log('add')
        // var vali = svgCaptcha.create({
        //    //options 
        // })
        // var vali = svgCaptcha.create({
        //   size: 4, // 验证码长度
        //   ignoreChars: '0o1i', // 验证码字符中排除 0o1i
        //   noise: 2 // 干扰线条的数量
        //   color: true, // 验证码的字符是否有颜色，默认没有，如果设定了背景，则默认有
        //   background: '#cc9966' // 验证码图片背景颜色
        // })
        var vali = svgCaptcha.createMathExpr({
          // size: 4, // 验证码长度
          ignoreChars: '0o1i', // 验证码字符中排除 0o1i
          noise: 3
        })
        console.log(vali.text)
        console.log(vali)

        this.session('name', {'aaaaa':13,'bb':'sdf'});
        // this.session('name', 'sdfsdf');
        let a = await this.session('name');
        console.log('a', a.bb)

        return
      }
      // const postId = this.get('postId');
      // const commentId = this.get('id');
      // const comment = this.model('comment');
      // if(commentId) { // 获取单条评论的详细信息
      //   const data = await comment.where({post_id: postId, id: commentId}).find();
      //   return this.success(data);
      // } else { // 获取单条文章下的评论列表
      //   const list = await comment.where({post_id: postId}).select();
      //   return this.success(list);
      // }
      let model = this.model('user');
      let data = await model.limit(2).select();
      // let cachedata = await model.cache('userList', {timeout: 2 * 3600 * 1000}).select();
      let cachedata = await model.where({email: '99312@qq.com'}).update({
        // sms_validity_time: ['exp', 'CURRENT_TIMESTAMP()']
        sms_validity_time: Math.round(new Date() / 1000)
      })
      this.ctx.body = {
          msg: 'ssssss'
        }
      console.log('headers', this.ctx.headers)
      console.log('data', data)
      console.log('time', Math.round(new Date()/1000))
      console.log('time2', Math.round(new Date()/1000) - new Date('2019-03-12 11:04:00').getTime()/1000)
      console.log('cachedata', cachedata)
      console.log('ip', this.ip.split(':')[3])
      console.log('id', this.id)
      console.log('di', this.get('di'))

          // Token 数据
  const payload = {
    name: 'wanghao',
    admin: true
  }

  // // 密钥
  const secret = 'ILOVENINGHAO'

  // // 签发 Token
  // const token = jwt.sign(payload, secret, { expiresIn: '1day' })

  // // 输出签发的 Token
  // console.log(token)
  const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoid2FuZ2hhbyIsImFkbWluIjp0cnVlLCJpYXQiOjE1NTIwMjgzODksImV4cCI6MTU1MjExNDc4OX0.PKNotP5HLw0PLexs7Uyc1KVwDcpeX7xk-DZIGTTrmaA'
  // const token = '13213.eyJuYW1lIjoid2FuZ2hhbyIsImFkbWluIjp0cnVlLCJpYXQiOjE1NTIwMjgzODksImV4cCI6MTU1MjExNDc4OX0.PKNotP5HLw0PLexs7Uyc1KVwDcpeX7xk-DZIGTTrmaA'

  // 验证 Token
  jwt.verify(token, secret, (error, decoded) => {
    if (error) {
      console.log(error.message)
      return
    }
    console.log(decoded)
  })

  }
};
